/**
 * Created by Administrator on 2016/4/6.
 */
$(document).ready(function(){
    $.extend({
        getParams: function() {
            var aQuery = window.location.href.split("?");
            var aGet = new Array();
            if (aQuery.length > 1) {
                var aParam = aQuery[1].split("&");
                for (var i = 0, aParamLength = aParam.length; i < aParamLength; i++) {
                    var aTemp = aParam[i].split("=");
                    aGet[aTemp[0]] = aTemp[1];
                }
            }
            return aGet;
        },
        isEmpty: function(obj) {
            for (var name in obj) {
                return false;
            }
            return true;
        },
        timeChange: function(source, inFormat, outFormat) {
            var checkTime = function(time) {
                if(time < 10) {
                    time = "0" + time;
                };
                return time;
            };
            switch(inFormat) {
                case 'Y-m-d H:i:s':
                    var reg =  /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
                    source = source.match(reg);
                    source = new Date(source[1], source[3]-1, source[4], source[5], source[6], source[7]);
                    break;
                case 'Y-m-d H:i':
                    var reg =  /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
                    source = source.match(reg);
                    source = new Date(source[1], source[3]-1, source[4], source[5], source[6]);
                    break;
                case 'Y-m-d':
                    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
                    source = source.match(reg);
                    source = new Date(source[1], source[3]-1, source[4]);
                    break;
                case 'timestamp':
                    source = new Date(parseInt(source)*1000);
                    break;
            };
            switch(outFormat) {
                case 'Y-m-d H:i:s':
                    return source.getFullYear() + '-'
                        + checkTime(source.getMonth()+1)
                        + '-'
                        + checkTime(source.getDate())
                        + ' '
                        + checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes())
                        + ':'
                        + checkTime(source.getSeconds());
                    break;
                case 'Y-m-d H:i':
                    return source.getFullYear() + '-'
                        + checkTime(source.getMonth()+1)
                        + '-'
                        + checkTime(source.getDate())
                        + ' '
                        + checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes());
                    break;
                case 'Y-m-d':
                    return source.getFullYear() + '-'
                        + checkTime(source.getMonth()+1)
                        + '-'
                        + checkTime(source.getDate());
                    break;
                case 'Y.m.d':
                    return source.getFullYear() + '.'
                        + checkTime(source.getMonth()+1)
                        + '.'
                        + checkTime(source.getDate());
                    break;
                case 'm-d H:i':
                    return checkTime(source.getMonth()+1)
                        + '-'
                        + checkTime(source.getDate())
                        + ' '
                        + checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes());
                    break;
                case 'm月d日 H:i':
                    return checkTime(source.getMonth()+1)
                        + '月'
                        + checkTime(source.getDate())
                        + '日 '
                        + checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes());
                    break;
                case 'm-d':
                    return checkTime(source.getMonth()+1)
                        + '-'
                        + checkTime(source.getDate());
                    break;
                case 'm.d':
                    return checkTime(source.getMonth()+1)
                        + '.'
                        + checkTime(source.getDate());
                    break;
                case 'm.d H:i':
                    return checkTime(source.getMonth()+1)
                        + '.'
                        + checkTime(source.getDate())
                        + ' '
                        + checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes());
                    break;
                case 'H:i':
                    return checkTime(source.getHours())
                        + ':'
                        + checkTime(source.getMinutes());
                    break;
                case 'timestamp':
                    return parseInt(source.getTime()/1000);
                    break;
                case 'newDate':
                    return source;
                    break;
                case 'Y/m/d':
                    return source.getFullYear() + '/'
                        + checkTime(source.getMonth()+1)
                        + '/'
                        + checkTime(source.getDate());
                    break;
            };
        },

        validator : {
            checkPhone : function(v){
                var pattern = new RegExp('^(13|15|18|14|17)[0-9]{9}$');
                var pattern1 = new RegExp('^([0-9]{3,4})?(-)?[0-9]{7,8}$');
                if(v == null || v == undefined || v == "") return;
                return pattern.test(v) || pattern1.test(v);
            },
            checkID : function(v){
                var pattern = new RegExp(/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[1-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}(\d|X)$/,'i');
                if( v == null || v == undefined || v == "" ) return false;
                return pattern.test(v);
            }

        },
        pictureUpload: function(options){ //webuploader 组件
            head.load('https://cdn.staticfile.org/webuploader/0.1.0/webuploader.html5only.min.js', function(){
                var opt = {
                    auto: true, //选择文件后马上上传
                    multiple: false,  //是否支持多文件
                    fileNumLimit: 50,
                    duplicate: true, //去重
                    swf: 'https://cdn.staticfile.org/webuploader/0.1.0/Uploader.swf', // swf文件路径
                    server: '/weixin/upload/picturePost', // 文件接收服务端。
                    pick: '#picker', // 选择文件的按钮。可选。内部根据当前运行是创建，可能是input元素，也可能是flash.
                    resize: false, // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                    accept: {
                        title: '图片',
                        extensions: 'gif,jpg,jpeg,bmp,png',
                        mimeTypes: 'image/*'
                    }
                };
                var settings = $.extend({}, opt, options);
                var webUploader = WebUploader.create(settings);
                webUploader.on('uploadSuccess', function(file, result){ //上传成功
                    if (settings.success) {
                        settings.success(result);
                    }
                });
                webUploader.on('uploadError', function (file, error) {//错误
                    if (settings.error) {
                        settings.error(error);
                    } else {
                        $.toast(error, 'forbidden');
                    }
                });
                webUploader.on( 'uploadProgress', function(file, percentage) {//上传进度
                    if(settings.progress){
                        settings.progress(file, percentage);
                    }
                });
                // 当有文件被添加进队列的时候
                var queued = settings.fileNumLimit > 1 ? 'filesQueued' : 'fileQueued';
                webUploader.on(queued, function(file) {
                    if( settings.loading == true ){
                        $.showLoading("上传图片中...");  //微信才能用这种方法
                    }
                });
                webUploader.on( 'uploadComplete', function(file) {
                    $.hideLoading();
                    if(settings.complete){
                        settings.complete(file);
                    }
                });
            });
        },
        submitting: 0,
        doAjax: function(options){  //ajax请求服务端
            var self = this;
            if (typeof options.submit_once !== 'undefined' && typeof options.submit_once && self.submitting === 1) return; //避免重复提交
            if(! options.url) {
                alert('请填写接口url');
                return false;
            }
            var opt = {
                type: 'post',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8'
            };
            var settings = $.extend({}, opt, options);
            if(settings.data){
                settings.data = ko.toJSON(settings.data);
            }

            settings.beforeSend = function() {
                self.submitting = 1;
                if(options.beforeSend){
                    options.beforeSend();
                }else{
                    $.showLoading('数据请求中...');
                }
            };
            settings.success = function(res) {
                $.hideLoading();
                if(options.success){
                    options.success(res);
                }
            };
            settings.error = function(res) {
                $.hideLoading();
                if(options.error){
                    options.error(res);
                }else{
                    $.alert('请求出错');
                }
            };
            settings.complete = function(res){
                self.submitting = 0;
                if(options.complete){
                    options.complete(res);
                }
            };
            $.ajax(settings);
        },
        getNowFormatDate: function(format) {
            var date = new Date();
            var separator1 = "-";
            var separator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();

            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            if (hour >= 0 && hour <= 9) {
                hour = "0" + hour;
            }
            if (minute >= 0 && minute <= 9) {
                minute = "0" + minute;
            }
            if (second >= 0 && second <= 9) {
                second = "0" + second;
            }
            var current_date = date.getFullYear() + separator1 + month + separator1 + strDate
                + " " + hour + separator2 + minute
                + separator2 + second;
            switch (format){
                case 'Y-m-d H:i':
                    current_date = date.getFullYear() + separator1 + month + separator1 + strDate
                        + " " + hour + separator2 + minute;
                    break;
                case 'Y-m-d':
                    current_date = date.getFullYear() + separator1 + month + separator1 + strDate;
                    break;
            }
            return current_date;
        },
    });
});